#define _CRT_SECURE_NO_WARNINGS 1
#include "queue.h"


//初始化
void queueinit(Queue* pq)
{
	assert(pq);
	pq->head = pq->tail = NULL;


}
//队尾入
void queuepush(Queue* pq, datatype x)
{
	assert(pq);
	queue* newnode = (queue*)malloc(sizeof(queue));
	newnode->data = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
		pq->tail = pq->head = newnode;
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;

	}


}
//队头出
void queuepop(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	//处理1个和多个
	if (pq->head->next == NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		queue* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}
	
}

//销毁
void destroy(Queue* pq)
{
	assert(pq);
	queue* cur = pq->head;
	while (cur)
	{
		queue* next = cur->next;
		free(cur);
		cur = next;
	}



}
//判空
bool empotyqueue(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}
//取队头
datatype queuehead(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->data;
}
//取队尾
datatype queuetail(Queue* pq)
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->data;
}
